package cn.jilian.meta.handler.generators;

import java.util.Map;
import org.apache.commons.lang3.StringUtils;

import cn.jilian.entity.Meta;
import cn.jilian.meta.handler.CompGenerator;
import cn.jilian.meta.handler.GeneratorManager;
import cn.jilian.meta.handler.constants.HandlerConstants;
import cn.jilian.meta.handler.domain.Model;
import cn.jilian.meta.handler.domain.Proxy;
import cn.jilian.meta.handler.domain.SimpleRestJsonStore;

public class SimpleRestJsonStoreGenerator implements CompGenerator<SimpleRestJsonStore> {

	private GeneratorManager manager;
	
	private CompGenerator<Model> modelGenerator; 
	
	public static final Map<Integer,String> typeMap=HandlerConstants.typeMap;
	
	public SimpleRestJsonStoreGenerator(GeneratorManager manager) {
		this.manager=manager;
		this.modelGenerator=manager.getGenerator(Model.class);
	}
	@Override
	public SimpleRestJsonStore convert(Meta meta, Map<String, Object> extConfig) {
		SimpleRestJsonStore store=new SimpleRestJsonStore();
		Model model=modelGenerator.convert(meta, null);
		String name=StringUtils.substringAfterLast(meta.getCode(), ".");
		name=StringUtils.capitalize(name);
		if(extConfig!=null&&extConfig.containsKey("app")) {
			name=new StringBuilder("")
					.append(extConfig.get("app"))
					.append(".")
					.append("store.")
					.append(name).toString();
		}else {
			name="Ext.ux.store."+name;
		}
		store.setName(name);
		store.setFields(model.getFields());
		Proxy proxy=new Proxy();
		if(meta.containsKey(Meta.PAGEQUERYURL)){
			String type=(String) meta.getAttr(Meta.PROXY_TYPE, Proxy.AJAX);
			proxy.setType(type);
			proxy.setUrl(meta.getAttr(Meta.PAGEQUERYURL,"../meta/query"));
		}
		return store;
	}

}
